import { acceptHMRUpdate, defineStore } from 'pinia'
import { store } from '../index'
import { hasChildrenMenuFn } from '@/router/utils/menuOperate'

export const useMenuStore = defineStore('menu', () => {
  const menuList: any = ref([])
  // 当前菜单的根级菜单是否有子菜单
  const hasChildrenMenu = ref(true)
  // 分栏模式时，分栏是否显示，用来计算侧边栏宽度，及判断分栏折叠按钮是否显示
  const isColumnShow = ref(false)

  const getMenuList = routes => {
    if (menuList.value.length > 0) {
      return
    }
    menuList.value = routes
  }

  // 判断根级菜单是否有子菜单
  const watchMenuChange = menu => {
    hasChildrenMenu.value = hasChildrenMenuFn(menuList.value, menu)
  }

  return {
    menuList,
    hasChildrenMenu,
    isColumnShow,
    getMenuList,
    watchMenuChange
  }
})

if (import.meta.hot) {
  import.meta.hot.accept(acceptHMRUpdate(useMenuStore, import.meta.hot))
}

export const useMenuStoreWithOut = () => {
  return useMenuStore(store)
}
